package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.conquests.Conquest;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class ConquestsResponse extends Response {
	
	protected List<ConquestInfo> list;
	private int size;
	private int maxSize;
//	public static class ConInfoEmployeeSampleInfo{
//		public long eid;
//		public String icon;
//		public ConInfoEmployeeSampleInfo(Employee e){
//			this.eid = e.getId();
//			this.icon = e.getIcon();
//		}
//	}
	
	public static class ConquestInfo {
		
		// 玩家信息
		public long ownerId;
		public String ownerName;
		// 建筑信息
		public int id;
		public String image;
		public String name;
		public int income;
		public int maxIncome;
		public byte state; // 0-不可领取，1-可领取
		public long[] employees;
//		public List<ConInfoEmployeeSampleInfo> employees;
		
		public ConquestInfo(Conquest c) {
			this.ownerId = c.getOwnerId();
			this.id = c.getId();
			Player player = App.getApp().getServiceManager().get(PlayerCacheService.class).find(c.getOwnerId());
//			Build build;
//			if (player != null && (build = player.getBuildings().getBuild(c.getId())) != null) {
//				this.ownerName = player.getName();
//				this.image = build.getTemplate().getImage(build.getLevel());
//				this.name = build.getTemplate().getName(build.getLevel());
//				this.income = build.getIncomePerMinute();
//				this.maxIncome = build.getMaxIncome();
//				this.state = (byte) (build.settle() >= this.maxIncome ? 1 : 0);
//			}
			BuildV2 build2;
			if(player != null && (build2 = player.getBuildings2().find(c.getId())) != null){
				this.ownerName = player.getName();
				this.image = build2.getTemplate().getImage();
				this.name = build2.getTemplate().getName();
				int[] moneyInfo = c.calcMoney(build2.getTemplate().getProduceMoneyFactor(), BuildV2.MAX_TAKE_MONEY_SECOND);
				this.income = moneyInfo[0];
				this.maxIncome = moneyInfo[1];
				this.state = (byte) (moneyInfo[2] > moneyInfo[0]? 0 : 1);
				if(this.maxIncome == 0){
					this.state = 0;
				}
				this.employees = new long[]{0, 0, 0, 0};
				if(this.employees.length >= c.getEmployees().length){
					for(int i = 0; i < c.getEmployees().length; i++){
						this.employees[i] = c.getEmployees()[i];
					}
				}
//				this.employees = new ArrayList<ConInfoEmployeeSampleInfo>();
//				for(int i = 0; i < c.getEmployees().length; i++){
//					long eid = c.getEmployees()[i];
//					if(eid > 0){
//						Employee e = player.getEmployees().get(eid);
//						ConInfoEmployeeSampleInfo info = new ConInfoEmployeeSampleInfo(e);
//						this.employees.add(info);
//					}
//				}
			}
		}
	}
	
	public ConquestsResponse(int serial, List<Conquest> conquests, int maxSize) {
		super(serial);
		if (conquests != null && conquests.size() > 0) {
			list = new ArrayList<ConquestInfo>(conquests.size());
			for (int i = 0; i < conquests.size(); i++) {
				Conquest c = conquests.get(i);
				list.add(new ConquestInfo(c));
			}
			this.size = conquests.size();
		} else {
			list = Collections.emptyList();
			this.size = 0;
		}
		this.maxSize = maxSize;//GameStaticConfig.captureBuildMaxCount;
	}

	public List<ConquestInfo> getList() {
		return list;
	}

	public void setList(List<ConquestInfo> list) {
		this.list = list;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}
	public int getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}
}
